#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\termstructures\volatility\swaption\cmsmarket.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Math;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Cashflows;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Termstructures { namespace Volatility { namespace Swaption {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICmsMarket
	public ref class CCmsMarket : 
            public System::MarshalByRefObject,
            public Cephei::QL::Termstructures::Volatility::Swaption::ICmsMarket
	{
	protected: 
		boost::shared_ptr<QuantLib::CmsMarket>* _ppCmsMarket;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CmsMarket>* _phCmsMarket;
#endif
		Object^ _CmsMarketOwner;     // reference to object that manages the storage for this object
	internal:
		CCmsMarket (Cephei::IVector<Cephei::QL::Times::IPeriod^>^ swapLengths, Cephei::IVector<Cephei::QL::Indexes::ISwapIndex^>^ swapIndexes, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::IMatrix<Cephei::QL::IQuote^>^ bidAskSpreads, Cephei::IVector<Cephei::QL::Cashflows::IHaganPricer^>^ pricers, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTS);
        CCmsMarket (boost::shared_ptr<QuantLib::CmsMarket>& childNative, Object^ owner);
        CCmsMarket (QuantLib::CmsMarket& childNative, Object^ owner);
        CCmsMarket (CCmsMarket^ copy);
        CCmsMarket (System::Type^ t);
#ifdef STRUCT
        CCmsMarket (QuantLib::CmsMarket childNative);
#endif       
#ifdef HANDLE
		CCmsMarket (QuantLib::Handle<QuantLib::CmsMarket>& childNative, Object^ owner);
		CCmsMarket (QuantLib::Handle<QuantLib::CmsMarket> childNative);
#endif
		virtual ~CCmsMarket ();
		!CCmsMarket ();

	internal:
		QuantLib::CmsMarket& GetReference ();
		boost::shared_ptr<QuantLib::CmsMarket>& GetShared ();
		QuantLib::CmsMarket* GetPointer ();
        void SetCmsMarket (boost::shared_ptr<QuantLib::CmsMarket> native)
        {
            if (_ppCmsMarket != NULL)
                delete _ppCmsMarket;
            _ppCmsMarket = new boost::shared_ptr<QuantLib::CmsMarket> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::CmsMarket>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property Cephei::QL::Math::IMatrix^ Browse 
        {
		    virtual Cephei::QL::Math::IMatrix^ get () ;
        }
        property Cephei::QL::Math::IMatrix^ ImpliedCmsSpreads 
        {
		    virtual Cephei::QL::Math::IMatrix^ get () ;
        }
        property Cephei::QL::Math::IMatrix^ SpreadErrors 
        {
		    virtual Cephei::QL::Math::IMatrix^ get () ;
        }
        property Cephei::IVector<Cephei::QL::Times::IPeriod^>^ SwapTenors 
        {
		    virtual Cephei::IVector<Cephei::QL::Times::IPeriod^>^ get () ;
        }
        property Cephei::QL::Termstructures::Volatility::Swaption::ICmsMarket^ Update 
        {
		    virtual Cephei::QL::Termstructures::Volatility::Swaption::ICmsMarket^ get () ;
        }
		virtual Double WeightedFwdNpvError (Cephei::QL::Math::IMatrix^ weights) ;
		virtual Double WeightedSpotNpvError (Cephei::QL::Math::IMatrix^ weights) ;
		virtual Double WeightedSpreadError (Cephei::QL::Math::IMatrix^ weights) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CCmsMarket_Factory : public System::MarshalByRefObject,  public ICmsMarket_Factory
	{
	public:
        virtual ICmsMarket^ Create (Cephei::IVector<Cephei::QL::Times::IPeriod^>^ swapLengths, Cephei::IVector<Cephei::QL::Indexes::ISwapIndex^>^ swapIndexes, Cephei::QL::Indexes::IIborIndex^ iborIndex, Cephei::IMatrix<Cephei::QL::IQuote^>^ bidAskSpreads, Cephei::IVector<Cephei::QL::Cashflows::IHaganPricer^>^ pricers, Cephei::QL::Termstructures::IYieldTermStructure^ discountingTS);
    };
   
/*Cephei*/ } /*QL*/ } /*Termstructures*/ } /*Volatility*/ } /*Swaption */}
